
package managedbean;

import entity.Employee;
import entity.TodoList;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.TodoListBeanLocal;

@ManagedBean(name="todoListManagerBean")
@ViewScoped
public class TodoListManagerBean implements Serializable{

    @EJB
    private TodoListBeanLocal todoListBeanLocal;
    
    private String task;
    private String statusMessage;
    private TodoList selectedTodoList;
    private List<TodoList> todoLists;
    
    public TodoListManagerBean() {
    }
    
    public void initView(PhaseEvent event)
    {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
        Employee employee = controller.getEmployee();
        System.out.println(employee.getUserName());
        todoLists = todoListBeanLocal.getTodoLists(employee.getEmployeeId());
    }
    
    public void create(ActionEvent event)
    {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
        Employee employee = controller.getEmployee();
        todoListBeanLocal.createToDoList(task, employee.getEmployeeId());
        statusMessage = "Add new task successfully.";
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
        task = "";
    }
    
    public void edit(ActionEvent event)
    {
        todoListBeanLocal.editToDoList(selectedTodoList.getTask(), selectedTodoList.getId());
        statusMessage = "Update new task successfully.";
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
    
    }
    
    public void delete(ActionEvent event)
    {
        todoListBeanLocal.deleteToDoList(selectedTodoList.getId());
        statusMessage = "Delete new task successfully.";
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    public TodoListBeanLocal getTodoListBeanLocal() {
        return todoListBeanLocal;
    }

    public void setTodoListBeanLocal(TodoListBeanLocal todoListBeanLocal) {
        this.todoListBeanLocal = todoListBeanLocal;
    }

    public TodoList getSelectedTodoList() {
        return selectedTodoList;
    }

    public void setSelectedTodoList(TodoList selectedTodoList) {
        this.selectedTodoList = selectedTodoList;
    }

    public List<TodoList> getTodoLists() {
        
        return todoLists;
    }

    public void setTodoLists(List<TodoList> todoLists) {
        this.todoLists = todoLists;
    }


}
